package cn.netbox.springmvc.controller;

import cn.netbox.springmvc.dao.UserDao;
import cn.netbox.springmvc.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.jws.WebParam;
import java.util.HashMap;
import java.util.Map;

/**
 * description:
 * date: 2022/3/26 19:49
 * author: Alex
 * version: 1.0
 */
@Controller
public class HelloController {

    @Autowired
    private UserDao userDao;

    @RequestMapping("/")
    public String Hello() {
        return "index";
    }

    @GetMapping("/getUser")
    public String getUserById(int id, Model model) {
        model.addAttribute("user", userDao.selectUserById(id));
        return "userInfo";
    }

    @GetMapping("/getAllUser")
    public String getAllUser(Model model) {
        model.addAttribute("userMapSet", userDao.getAllUser());
        return "userlist_info";
    }

    @PostMapping("/user")
    public String createUser(User user, Model model) {
        userDao.create(user);
        return "success";
    }

    @DeleteMapping("/delete")
    public String deleteUser(int id) {
        userDao.deletUserById(id);
        return "success";
    }
    @GetMapping("/toUpdate/{id}")
    public String toUpdate(@PathVariable("id")int id,Model model){
        User user = userDao.selectUserById(id);
        System.out.println(user);
        model.addAttribute("user",user);
        return "update";
    }

    @PutMapping("/update")
    public String update(User user){
        userDao.updateUser(user);
        return "success";
    }

    @GetMapping("/toAdd")
    public String toAdd(){
        return "add";
    }

    @PostMapping("/add")
    public String add(User user,Model model){
        userDao.create(user);
        return "redirect:/getAllUser";
    }
}
